package com.jgssp.ad.sdk.mediation.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.interstitial.MediationCustomInterstitialLoader;
import com.bytedance.sdk.openadsdk.mediation.custom.MediationCustomServiceConfig;
import com.jgssp.ad.sdk.mediation.utils.PriceUtil;
import com.jgssp.ad.sdk.mediation.utils.ThreadUtils;

import java.util.Map;

import cn.jiguang.jgssp.ad.ADJgInterstitialAd;
import cn.jiguang.jgssp.ad.data.ADJgInterstitialAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgInterstitialAdListener;

/**
 * @Description: 插屏广告适配器
 */
public class CustomerInterstitialAdapter extends MediationCustomInterstitialLoader {

    private ADJgInterstitialAd interstitialAd;
    private ADJgInterstitialAdInfo interstitialAdInfo;

    @Override
    public void load(final Context context, AdSlot gmAdSlotInterstitial, final MediationCustomServiceConfig gmCustomServiceConfig) {
        ThreadUtils.runOnUIThreadByThreadPool(new Runnable() {
            @Override
            public void run() {

                if (!(context instanceof Activity)) {
                    callLoadFail(-1, "JgSSP Context must be Activity");
                    return;
                }

                interstitialAd = new ADJgInterstitialAd((Activity) context);
                interstitialAd.setListener(new ADJgInterstitialAdListener() {
                    @Override
                    public void onAdReceive(ADJgInterstitialAdInfo adInfo) {
                        interstitialAdInfo = adInfo;
                        if (isClientBidding() && interstitialAdInfo != null) {//bidding广告类型
                            //bidding广告成功回调，回传竞价广告价格 单位：分
                            callLoadSuccess(PriceUtil.toCent(interstitialAdInfo.getECPM()));
                        } else {
                            //普通广告成功回调
                            callLoadSuccess();
                        }
                    }

                    @Override
                    public void onAdExpose(ADJgInterstitialAdInfo adInfo) {
                        callInterstitialShow();
                    }

                    @Override
                    public void onAdClick(ADJgInterstitialAdInfo adInfo) {
                        callInterstitialAdClick();
                    }

                    @Override
                    public void onAdClose(ADJgInterstitialAdInfo adInfo) {
                        callInterstitialClosed();
                    }

                    @Override
                    public void onAdFailed(ADJgError error) {
                        callLoadFail(error.getCode(), error.getError());
                    }

                    @Override
                    public void onAdReady(ADJgInterstitialAdInfo adJgInterstitialAdInfo) {

                    }

                });
                if (gmCustomServiceConfig != null) {
                    interstitialAd.loadAd(gmCustomServiceConfig.getADNNetworkSlotId());
                }
            }
        });

    }

    /**
     * show在主线程中回调
     */
    @Override
    public void showAd(final Activity activity) {
        Log.i(Const.TAG, "showAd " + Thread.currentThread().toString());
        if (interstitialAdInfo != null && activity != null && !activity.isFinishing()) {
            interstitialAdInfo.showInterstitial(activity);
        }
    }

    @Override
    public MediationConstant.AdIsReadyStatus isReadyCondition() {
        if (interstitialAdInfo != null && interstitialAdInfo.isReady()) {
            return MediationConstant.AdIsReadyStatus.AD_IS_READY;
        } else {
            return MediationConstant.AdIsReadyStatus.AD_IS_NOT_READY;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (interstitialAd != null) {
            interstitialAd.release();
            interstitialAd = null;
        }
        if (interstitialAdInfo != null) {
            interstitialAdInfo.release();
            interstitialAdInfo = null;
        }
    }

    @Override
    public void receiveBidResult(boolean win, double winnerPrice, int loseReason, Map<String, Object> extra) {
    }

    /**
     * 是否clientBidding广告
     *
     * @return
     */
    public boolean isClientBidding() {
        return getBiddingType() == MediationConstant.AD_TYPE_CLIENT_BIDING;
    }

}
